package com.wf.course.web.controller.comm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.utils.page.PageBean;
import com.wf.course.model.comm.Notice;
import com.wf.course.service.INoticeService;
import com.wf.course.web.Interceptor.LogInfo;

/**
 *公告管理
 */

@RequestMapping("notice")
@Controller
public class NoticeController {

	@Autowired
	private INoticeService noticeService;

	
	 /***
	  * 公告管理
	  * @param pageNo
	  * @param pageSize
	  * @param title
	  * @param model
	  * @return
	  */
	@LogInfo("查询公告列表信息")
	@RequestMapping("noticeList")
	public String imgList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
						  @RequestParam(value = "pageSize", required = false) Integer pageSize,
						  @RequestParam(value = "noticeTitle", required = false) String noticeTitle,
						  Model model){
		Map<String,Object> map=new HashMap<String,Object>(); 
		map.put("noticeTitle",noticeTitle);
		
		PageBean<Notice> pageBean = new PageBean<>(pageNo, pageSize,null,map);
		noticeService.getNoticeList(pageBean);
		
		pageBean.setUrl("/notice/noticeList");
		model.addAttribute("page", pageBean);
		return "comm/noticeList";
	}
	
	/**
	 * 公告：新增修改页面信息加载
	 * @param id
	 * @return
	 */
	@LogInfo("添加或修改公告信息，页面数据初始化")
	@RequestMapping("downLoadNotice/{id}")
	public String downLoadNotice(@PathVariable("id") Integer id,Model model){
		 if(id!=null && id!=0){
			 Notice notice = this.noticeService.downLoadNotice(id);
				model.addAttribute("notice", notice);
				model.addAttribute("type", "upd");
		 }else{
			 model.addAttribute("type", "add");
		 }
		 
		return "comm/noticeEdit";	
		
	}
	
	
	/**
	 * 添加或修改
	 * @param notice
	 * @param request
	 * @return
	 */
	@LogInfo("添加/修改公告信息")	 
	@RequestMapping(value = {"noticeEdit" } )
	public String noticeEdit(@ModelAttribute Notice notice, 
			HttpServletRequest request){
		 
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			notice.setNoticeTime(sdf.format(new Date()));

			//添加信息公告
			if(notice.getId()==null){
				//保存
				noticeService.saveNotice(notice);
			}else{
				//修改
				noticeService.updateNotice(notice);
				
			}
			 
		 
		return "redirect:/notice/noticeList";
		
	}
	
	/**
	 * 删除公告
	 * @param id
	 * @return
	 */
	@LogInfo("删除公告信息")	 
	@RequestMapping(value="deleteNotice/{id}"  )
	public String deleteNotice(@PathVariable Integer id){
			
			this.noticeService.deleteNotice(id);
			
			return "redirect:/notice/noticeList";
	}
	
}
